package com.feng.cloud.common.converter;

import com.feng.cloud.common.utils.DateUtils;
import com.wuwenze.poi.convert.WriteConverter;
import com.wuwenze.poi.exception.ExcelKitWriteConverterException;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName TimeConverter
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/18 14:10
 * @Version 1.0
 **/
@Slf4j
public class TimeConverter implements WriteConverter {
    @Override
    public String convert(Object value) throws ExcelKitWriteConverterException {
        if (value == null)
            return "";
        else {
            try {
                if (value instanceof LocalDateTime) {
                    DateTimeFormatter df = DateTimeFormatter.ofPattern(DateUtils.FULL_TIME_SPLIT_PATTERN);
                    LocalDateTime localDateTime = (LocalDateTime) value;
                    return localDateTime.format(df);
                } else if (value instanceof LocalDate) {
                    DateTimeFormatter df = DateTimeFormatter.ofPattern(DateUtils.FULL_DATE_SPLIT_PATTERN);
                    LocalDate localDate = (LocalDate) value;
                    return localDate.format(df);
                }
                return "";
            } catch (Exception e) {
                String message = "时间转换异常";
                log.error(message, e);
                throw new ExcelKitWriteConverterException(message);
            }
        }
    }
}
